var express = require('express');
var router = express.Router();

const infosService = require('../service/infosService');

//查询所有
router.get('/', async function (req, res) {
    let { type, value, current, pageSize } = req.query;
    let searchParams = {};
    searchParams[type] = value;
    let data = await infosService.find({ current: current || 1, pageSize: pageSize || 5 }, searchParams);
    res.send(data);
});

//新增
router.post('/', async function (req, res) {
    let { title, createTime, content, imgs } = req.body;//获取数据
    let data = await infosService.insert({ title, createTime, watches:0 ,content, imgs });
    res.send(data);
});

//按id查询
router.get('/:id', async function (req, res) {
    let { id } = req.params;//获取路径后面的变量用params
    let data = await infosService.findById(id);
    res.send(data);
});

//删除
router.delete('/:id', async function (req, res) {
    let { id } = req.params;
    let data = await infosService.delete(id);
    res.send(data);
});

//修改
router.put('/:id', async function (req, res) {
    let { id } = req.params;
    let { title, createTime, content, imgs } = req.body;
    let data = await infosService.update({ _id: id, title, createTime, watches, content, imgs });
    res.send(data);
});

module.exports = router;//把路由暴露出去